package exercise;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA
 * Description：使用ThreadPoolExecutor创建一个忽略最新任务的线程池，创建规则：
 * 1.核心线程数为5
 * 2.最大线程数为10
 * 3.任务队列为100
 * 4.拒绝策略为忽略最新任务
 * User：chenshu
 * Date：2023-11-16
 * Time：11:57
 */
public class Demo4 {
    public static Object locker = new Object();
    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                5,
                10,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                new ThreadPoolExecutor.DiscardPolicy());

        AtomicInteger sum = new AtomicInteger();
        for (int i = 0; i < 1000; i++) {
            int n = i;
            threadPool.submit(() -> {
                System.out.println("task" + n + "已执行！");
                sum.getAndIncrement();
            });
        }
        threadPool.shutdown();
        while(!threadPool.isTerminated()) {
            System.out.println("threadPool is running");
        }
        System.out.println(sum);
    }
}
